/*
* Copyright 2017 Baiyyy
* @(#)ApiLoginController.java
*/

package com.byyy.ccts.modules.api.controller;

import com.byyy.ccts.core.base.ILogBase;
import com.byyy.ccts.core.common.controller.BaseApiController;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.utils.JwtUtils;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.sys.entity.*;
import com.byyy.ccts.modules.sys.security.shiro.realm.UserRealm;
import com.byyy.ccts.modules.sys.service.IDictService;
import com.byyy.ccts.modules.sys.service.IUserService;
import com.byyy.ccts.modules.sys.service.impl.PasswordService;
import com.byyy.ccts.modules.sys.utils.UserUtils;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Created by DELL on 2019-8-20.
*/
@Controller
@RequestMapping("/api/user")
public class ApiUserController extends BaseApiController {

    @Autowired
    private IUserService userService;

    /**
     * 根据用户名称获取用户对应的货主权限
     * @param username
     * @return
     */
    @RequestMapping(value = "/queryYezByUsername", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson queryYezByUsername(String username) {
        AjaxJson ajaxJson = new AjaxJson();
        List<Yez> yezList = this.userService.queryYezByUsername(username);
        ajaxJson.setData(yezList);
        return ajaxJson;
    }

    /**
     * 根据用户名称获取用户对应的货主仓库权限
     * @param username
     * @param yezId
     * @return
     */
    @RequestMapping(value = "/queryYezCangkuByUsername", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson queryYezCangkuByUsername(String username, String yezId) {
        AjaxJson ajaxJson = new AjaxJson();
        List<Cangku> cangkuList = this.userService.queryYezCangkuByUsername(username, yezId);
        ajaxJson.setData(cangkuList);
        return ajaxJson;
    }

    /**
     * 获取当前登录用户登录仓库和仓库权限列表
     * @return
     */
    @RequestMapping(value = "/queryLoginCangkuAndList", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson queryLoginCangkuAndList() {
        AjaxJson ajaxJson = new AjaxJson();
        LinkedHashMap map = new LinkedHashMap();
        UserRealm.Principal principal = UserUtils.getPrincipal();
        List<Cangku> cangkuList = this.userService.queryYezCangkuByUsername(principal.getUsername(), principal.getYezId());
        ajaxJson.setData(cangkuList);
        map.put("cangku", principal.getCangku());
        ajaxJson.setExtend(map);
        return ajaxJson;
    }

    /**
     * 修改当前登录用户所属仓库
     * @return
     */
    @RequestMapping(value = "/changeLoginCangku", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson changeLoginCangku(String cangku) {
        AjaxJson ajaxJson = new AjaxJson();
        UserRealm.Principal principal = UserUtils.getPrincipal();
        if (principal != null) {
            principal.setCangku(cangku);
        }
        else {
            ajaxJson.fail("登陆账户失效！请重新登陆！");
        }
        return ajaxJson;
    }

}
